package com.lukk.util.jpush;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jsms.api.SendSMSResult;
import cn.jsms.api.common.SMSClient;
import cn.jsms.api.common.model.SMSPayload;

/**
 * 
 * JSMSUtil 上午10:54:48 Copyright zhaocj Inc. All Rights reserved.
 * alldios@outlook.com
 *
 */
public class JSMSUtil {

	protected static final Logger LOG = LoggerFactory.getLogger(JSMSUtil.class);

	private static final String appkey = "3e77ad1648b09e2ec3cc0c01";
	private static final String masterSecret = "9acdbbf17b76fa4dc5c74336";

	/**
	 * 
	 * @param phoneNumber
	 *            为手机发送信息
	 * @return
	 */
	public static SendSMSResult sendPromoteMessageToUser(String phoneNumber) {
		SMSClient client = new SMSClient(masterSecret, appkey);
		SMSPayload payload = SMSPayload.newBuilder().setMobildNumber(phoneNumber).setTempId(49523)
				.addTempPara("colon", ":").build();
		SendSMSResult res = null;
		try {
			res = client.sendTemplateSMS(payload);
			LOG.info(res.toString());
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Message: " + e.getMessage());
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		}
		return res;
	}

	/**
	 * 
	 * @param phoneNumber
	 * @param sendType
	 *            1为已经通过 2为还未通过
	 * @return
	 */
	public static SendSMSResult sendVerifyWithdrawal(String phoneNumber, Integer sendType) {

		SMSClient client = new SMSClient(masterSecret, appkey);
		SendSMSResult res = null;
		SMSPayload payload = null;
		if (1 == sendType) {
			payload = SMSPayload.newBuilder().setMobildNumber(phoneNumber).setTempId(44567).addTempPara("comma", "，")
					.build();
		} else {
			payload = SMSPayload.newBuilder().setMobildNumber(phoneNumber).setTempId(44664).addTempPara("comma", "，")
					.build();
		}
		try {
			res = client.sendTemplateSMS(payload);
			LOG.info(res.toString());
		} catch (APIRequestException e) {
			LOG.error("Error response fr om JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Message: " + e.getMessage());
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		}
		return res;
	}

	@Test
	public void testSend() {
		sendPromoteMessageToUser("13233844503");
	}
}
